Read a file and store lines into a variableΒΆ
Read a file line by line store it into a variable.
def file_read(fname):
with open (fname, "r") as f:
var = f.readlines()
print(var)
# test
file_read('test.txt')
Output:
['Welcome to w3resource.com.\n', 'Append this text.Append this text.Append this text.\n', 'Append this text.\n
', 'Append this text.\n', 'Append this text.\n', 'Append this text.\n']